import { defineStore } from "pinia";
import { getAllUsersAPI } from "@/service/login/user";
import cache from "@/utils/cache";
import Swal from "sweetalert2";
import { ref, Ref } from "vue";
import { format } from "date-fns";

export const useUserListStore = defineStore("userList", {
  state: () => ({
    usersList: ref([]) as Ref<any>,
  }),
  actions: {
    async getAllUsers() {
      const token = cache.getCache("token");
      const res = await getAllUsersAPI(token);
      this.usersList.value = res.data.map((user: any) => ({
        ...user,
        createdAt: user.createdAt
          ? format(new Date(user.createdAt), "yyyy-MM-dd HH:mm:ss")
          : format(new Date(), "yyyy-MM-dd HH:mm:ss"), // 格式化为 'yyyy-MM-dd HH:mm:ss'
      }));
      Swal.fire({
        title: "获取用户列表成功",
        icon: "success",
      });
    },
  },
});
